package com.gonsin.gview.example.gview.user;

import com.gonsin.base.GPageRequest;
import com.gonsin.base.GPageResult;
import com.gonsin.gview.annotations.GView;
import com.gonsin.gview.annotations.actions.GAction;
import com.gonsin.gview.annotations.board.GBoard;
import com.gonsin.gview.annotations.board.GButton;
import com.gonsin.gview.annotations.board.form.*;
import com.gonsin.gview.annotations.data.GDataSource;
import com.gonsin.gview.annotations.data.GListData;
import com.gonsin.gview.annotations.enums.InputType;
import com.gonsin.gview.example.bean.GroupBean;
import com.gonsin.gview.example.bean.UserBean;
import com.gonsin.gview.example.dao.GroupDao;
import com.gonsin.gview.example.dao.UserDao;
import com.gonsin.gview.example.utils.PageableUtils;
import com.gonsin.gview.logic.enums.FileUploadType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Component;

import javax.validation.Valid;
import java.util.List;

import static com.gonsin.gview.annotations.enums.ButtonAction.CLOSE_DIALOG;
import static com.gonsin.gview.annotations.enums.ButtonAction.CONFIRM_AND_CLOSE;

@GView(
        viewName = "AddUserView",
        title = "添加用户",
        boards = {
                @GBoard(form = @GForm(
                        formItems = {
                                // 通过直接执行sql语句获取数据
                                @GFormItem(selector = @GSelector(name = "选择权限组", keyName = "groupId", listData = @GListData(
                                        fromSql = "select id as groupId, name from gs_user_group",
                                        keyName = "groupId",
                                        valueName = "groupId",
                                        titleName = "name"
                                ))),
                                // 通过数据源名称获取数据
                                @GFormItem(selector = @GSelector(name = "选择权限组", keyName = "groupId", listData = @GListData(
                                        fromDataSource = "searchGroup",
                                        keyName = "groupId",
                                        valueName = "groupId",
                                        titleName = "name"
                                ))),
                                @GFormItem(textInput = @GTextInput(name = "真实名字", keyName = "realName")),
                                @GFormItem(textInput = @GTextInput(name = "用户名", keyName = "username")),
                                @GFormItem(tag = @GTag(name = "标签", keyName = "tags")),
                                @GFormItem(delaySelector = @GDelaySelector(name = "毫秒数", keyName = "delay")),
                                @GFormItem(file = @GFile(name = "上传文件夹", keyName = "path", fileUploadType = FileUploadType.PATH)),
                                @GFormItem(textInput = @GTextInput(name = "密码", keyName = "password", inputType = InputType.PASSWORD)),
                                @GFormItem(textInput = @GTextInput(name = "确认密码", keyName = "password", inputType = InputType.RE_PASSWORD)),
                        },
                        okButton = @GButton(action = CONFIRM_AND_CLOSE, actionName = "addUser", name = "确定"),
                        cancelButton = @GButton(action = CLOSE_DIALOG, name = "取消")
                ))
        }
)
@Component
public class AddUserView {

    @Autowired private GroupDao groupDao;
    @Autowired private UserDao userDao;

    @GDataSource("searchGroup")
    public List<GroupBean> searchGroup(){
        return groupDao.findAll();
    }

    /**
     * 查询用户组
     */
    @GDataSource("pageGroup")
    public GPageResult<GroupBean> pageGroup(GPageRequest pageRequest){
        Page<GroupBean> data = groupDao.findAll(PageableUtils.toPageable(pageRequest));
        return PageableUtils.toGPageResult(data);
    }

    @GAction("addUser")
    @Valid
    public void addUser(UserBean user){
        user.setUserId(0);
        userDao.saveAndFlush(user);
    }

}
